
//var url = 'http://feeds.feedburner.com/nerdcore-mp3';
var url = 'http://feeds.feedburner.com/univas/tecnologia';
Titanium.include('strip_tags.js');  
///////////////////////////////////////////////////////
var data;
var i = 0;
var feedTableView;
var feedTitle = '';
var stream = Ti.Media.createAudioPlayer();
var item_window = Ti.UI.createView({
        backgroundColor:'#4682B4',
	borderRadius:8,
	right:5,
	left:5,
	height:50,
	bottom:5
});
if (Ti.Platform.osname == 'iphone'){
	var aviso = Titanium.UI.createActivityIndicator({
   	 bottom:50, 
 	   height:55,
 	   width:190,
 	   color:'#FFFFFF',
  	  backgroundColor:'#000',
  	  opacity:0.9,
  	  borderRadius:5,
  	  borderColor:'#000',
  	  font:{fontFamily:'Helvetica Neue', fontSize:13},
  	  message:'Espere Porfavor...',	
  	 //style:Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN
	});
}
else {
var aviso = Titanium.UI.createActivityIndicator({
   	 height:40,
   	 width:10,
   	 message:'Espere Porfavor...',	
	});
}      
aviso.show();
Titanium.UI.currentWindow.add(aviso);
Ti.UI.currentWindow.add(item_window);
var item_title_label = Ti.UI.createLabel({
	text: '',
	color: '#fff',
	textAlign:'center',
	left:10,
	right:10,
	top:5,
	height:45,
	font:{fontFamily:'Helvetica Neue',fontWeight:'bold',fontSize:18}
	});
item_window.add(item_title_label);
var item_desc_label = Ti.UI.createLabel({
	text: '',
	color: '#000',
	textAlign:'center',
	left:10,
	right:10,
	top:55,
	height:40,
	font:{fontFamily:'Helvetica Neue',fontWeight:'bold',fontSize:13}
	});
item_window.add(item_desc_label);
function displayItems(itemList){
	for (var c=0;c < itemList.length;c++){	
		var title = null;
		var desc = null;
		var mp3_url = null;
		
		// If we want to only add items with mp3 enclosures
		if(itemList.item(c).getElementsByTagName("enclosure")!=null){

			// Item title
			title = itemList.item(c).getElementsByTagName("title").item(0).text;
			// Item description
			desc = itemList.item(c).getElementsByTagName("description").item(0).text;
			// Clean up any nasty linebreaks in the title and description			
			title = title.replace(/\n/gi, " ");			
			desc = desc.replace(/\n/gi, " ");

			// Podcast mp3 enclosure
			mp3_url = itemList.item(c).getElementsByTagName("enclosure").item(0).getAttribute("url");

			// Create a table row for this item
			var row = Ti.UI.createTableViewRow({
				height:'auto',
				//backgroundColor:'#eeeeee',
				//selectedBackgroundColor:'#b40000'
				}); 

			// Create a label for the title
			var post_title = Ti.UI.createLabel({
				text: title,
				color: '#000',
				textAlign:'left',
				left:60,
				height:'auto',
				width:'auto',
				top:3,
				font:{fontWeight:'bold',fontSize:16}
			});
			row.add(post_title);
			
			// add the CNN logo on the left
			// naturally this could be an image in the feed itself if it existed
			var item_image = Ti.UI.createImageView({
				image:'../imgs/audio.png',
				left:3,
				top:2,
				width:48,
				height:48
			});
			row.add(item_image);
			
			// Add some rowData for when it is clicked			
			row.thisTitle = title;
			row.thisMp3 = mp3_url;
			row.thisDesc = desc;
			
			// Add the row to the data
			data[i] = row;
			// I use 'i' here instead of 'c', as I'm only adding rows which have mp3 enclosures
			i++;
			
		} // end if enclosure		
	}
	
	// create the table
	feedTableView = Titanium.UI.createTableView({
		data:data,
		top:0,
		//width:320,
		height:260

	});
	
	// Add the tableView to the current window
	Titanium.UI.currentWindow.add(feedTableView);
	
	// Create tableView row event listener
	feedTableView.addEventListener('click', function(e){

		// a feed item was clicked
		Ti.API.info('item index clicked :'+e.index);
		Ti.API.info('title  :'+e.rowData.thisTitle);
		Ti.API.info('description  :'+strip_tags(e.rowData.thisDesc));
		Ti.API.info('mp3 enclosure  :'+e.rowData.thisMp3);
		// show an alert
		// Ti.UI.createAlertDialog({title:e.rowData.thisTitle, message:e.rowData.thisMp3}).show();
		item_title_label.text = strip_tags(e.rowData.thisTitle);
		item_desc_label.text = strip_tags(e.rowData.thisDesc);
		// etc ...	
		// now do some cool stuff! :)
		// like add an audio player, open a new window, etc..		
		var ventana = Titanium.UI.createWindow({});
		var pag = Titanium.UI.createWebView({
			html:	'<!DOCTYPE html><meta http -equiv= "content type" content = "text/html; charset= utf-8"/><html><br><center><h5>...'+e.rowData.thisTitle+'...</h5><br>'+e.rowData.thisDesc+'<br><br><h5> Para escuchar el Podcast de click en la liga de abajo</h5></center></html>'		
		});
		var ta = Ti.UI.createLabel({
			autoLink: 1,
			left:5, bottom: 0, right: 5, height: 80,
			editable: false, // this needs to be set to false, otherwise data detection type will fail
			backgroundColor:'#ccc',
			text:e.rowData.thisMp3
			//html:' <!DOCTYPE html><html><a href="'+e.rowData.thisMp3+'" >Escuchar podcast</a></html>'
		});
		ventana.add(pag);
		ventana.add(ta); 
		var back  = Titanium.UI.createButton({
			title: "regresar"		
		});
		ventana.setLeftNavButton(back);
		back.addEventListener('click', function (e){
			ventana.close();
		});
		ventana.open({modal:true});
		//#######boorar
/*
		stream.stop();
		stream.url = e.rowData.thisMp3;
                //////////////////////////////$$$$$$$$$$$$$$$$$$$$
		//stream.allowBackground =true;
		//stream.audioSessionMode = Titanium.Media.AUDIO_SESSION_MODE_AMBIENT; 
		//////////////////////////////$$$$$$$$$$$$$$$$$$$$
		stream.start();
		if(Ti.Platform.osname == 'iphone'){
			play.show();
			pause.show();
       	        	stop.show();

		}
*/	 
	});
}
function loadRSSFeed(url){

	data = [];
	Ti.API.info('>>>> loading RSS feed '+url);
	xhr = Titanium.Network.createHTTPClient();
	xhr.open('GET',url);
	xhr.onload = function()
	{			
		Ti.API.info('>>> got the feed! ... ');		
		// Now parse the feed XML 
		var xml = this.responseXML;	
		// Find the channel element 
		var channel = xml.documentElement.getElementsByTagName("channel");
		feedTitle = channel.item(0).getElementsByTagName("title").item(0).text;
		
		Ti.API.info("FEED TITLE " + feedTitle);
		
		//Titanium.UI.currentWindow.title = feedTitle;
		// Find the RSS feed 'items'
		var itemList = xml.documentElement.getElementsByTagName("item");
		Ti.API.info('found '+itemList.length+' items in the RSS feed');

		item_title_label.text = 'Univas Podcast';
		item_desc_label.text = 'Selecciona el Podcast a reproducir';
		
		// Now add the items to a tableView
		displayItems(itemList);
		aviso.hide();
	};
	
	item_title_label.text = 'Cargando lista de Podcast..';
	item_desc_label.text = '...';
	xhr.send();
	
}
// load the feed
loadRSSFeed(url);
// RIGHT NAVBAR REFRESH BUTTON	
if (Ti.Platform.osname == 'iphone'){	
	var r = Titanium.UI.createButton({
		systemButton:Titanium.UI.iPhone.SystemButton.REFRESH
	});
	r.addEventListener('click',function()
	{
	// reload feed
		loadRSSFeed(url);	
	});
	Titanium.UI.currentWindow.setRightNavButton(r);
	////pause stop stream
	//botones de play pause y stop

	//PLAY
	var play = Titanium.UI.createButtonBar({
		labels:['PLAY'],
  	      backgroundColor:'#006400',
		style:Titanium.UI.iPhone.SystemButtonStyle.BAR,
		//systemButton:Titanium.UI.iPhone.SystemButton.REFRESH,
		left:45,
		height:25,
		width:70
	});
	play.addEventListener('click',function()
	{
	//////////////////////////////$$$$$$$$$$$$$$$$$$$$
	stream.audioSessionMode = Ti.Media.AUDIO_SESSION_MODE_PLAYBACK; 
	//////////////////////////////$$$$$$$$$$$$$$$$$$$$
		stream.start();	
	});
	play.hide();
	item_window.add(play);
	//pause
	var pause = Titanium.UI.createButtonBar({
		labels:['PAUSE'],
		backgroundColor:'#CD853F',
		style:Titanium.UI.iPhone.SystemButtonStyle.BAR,
		//systemButton:Titanium.UI.iPhone.SystemButton.REFRESH,
		left:125,
		height:25,
		width:70
	});
	pause.addEventListener('click',function()
	{

		stream.pause();
	});
	pause.hide();
	item_window.add(pause);

	////STOP
	var stop = Titanium.UI.createButtonBar({
		labels:['STOP'],
		backgroundColor:'#800000',
		style:Titanium.UI.iPhone.SystemButtonStyle.BAR,
		//systemButton:Titanium.UI.iPhone.SystemButton.REFRESH,
		left:205,
		height:25,
		width:70
	});
	stop.addEventListener('click',function()
	{
		stream.stop();
		play.hide();
		pause.hide();
		stop.hide();
	});
	stop.hide();
	item_window.add(stop);
}
////find e botyonoes pause play y stop





